package com.telit.firecommand.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.telit.common.entity.DataReturn;
import com.telit.common.entity.PageData;
import com.telit.firecommand.dto.NoticePushDTO;
import com.telit.firecommand.entity.CommMsgPushReceiver;
import com.telit.firecommand.service.MsgPushReceiverService;
import com.telit.firecommand.service.SmsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Create by Chencf 2023/01/05
 */
@RestController
@Api(tags = "推送消息管理")
@RequestMapping("receiver")
public class MsgPushReceiverController {

    @Autowired
    private SmsService smsService;

    @Autowired
    private MsgPushReceiverService msgPushReceiverService;

    @GetMapping(value = "/listByPage",produces = "application/json;charset=UTF-8")
    @ApiOperation(value = "分页查询推送人员管理")
    public DataReturn listByPage(@RequestParam(defaultValue = "1") Integer current,
                                 @RequestParam(defaultValue = "10") Integer size,
                                 @RequestParam(required = false,value = "receiverName") String receiverName){
        IPage<CommMsgPushReceiver> pageList = msgPushReceiverService.listByPage(current,size,receiverName);
        return DataReturn.ok(new PageData<>((int)pageList.getTotal(),current,size,pageList.getRecords()));
    }

    @PostMapping(value = "/add",produces = "application/json;charset=UTF-8")
    @ApiOperation(value = "新增")
    public DataReturn add(@RequestBody CommMsgPushReceiver commMsgPushReceiver){
        return DataReturn.ok(msgPushReceiverService.add(commMsgPushReceiver));
    }

    @PutMapping(value = "/update/{id}",produces = "application/json;charset=UTF-8")
    @ApiOperation(value = "修改")
    public DataReturn update(@PathVariable("id") Long id,
                             @RequestBody CommMsgPushReceiver commMsgPushReceiver){
        commMsgPushReceiver.setId(id);
        return DataReturn.ok(msgPushReceiverService.update(commMsgPushReceiver));
    }

    @DeleteMapping(value = "/deleteBatch", produces = "application/json;charset=UTF-8")
    @ApiOperation(value = "批量删除")
    public DataReturn deleteBatch(@RequestParam("ids") List<Long> ids){
        return DataReturn.ok(msgPushReceiverService.deleteBatch(ids));
    }

    @PostMapping(value = "/pushUsersMsg")
    @ApiOperation(value = "推送短信给用户")
    public DataReturn pushUsersMsg(@RequestParam("fireId") String fireId) throws Exception {
        NoticePushDTO noticePushDTO = new NoticePushDTO();
        List<String> mobileList = msgPushReceiverService.selectMobileListByType();
        noticePushDTO.setFireId(fireId);
        noticePushDTO.setStatus(1);
        noticePushDTO.setMobileList(mobileList);
        smsService.sendSms(noticePushDTO);
        return DataReturn.ok();
    }
}
